<?php

namespace App\Http\Controllers\Guard\Car;

use App\Http\Controllers\Common\GuardController;
use App\Models\Car\RentUnit;
use Illuminate\Http\Request;

class RentUnitController extends GuardController{


    public function __construct() {
        $this->instance = new RentUnit();
    }

    public function lst(Request $request) {
        if($request->has('all')){
            $records = $this->instance->getLst()->get(['id','unit_name']);
            return responseSuccess($records->toArray());
        }

        $page = $request->input('page',1);
        $pageSize = $request->input('pageSize',20);
        $conditions = $request->input();
        $records = $this->instance->getLst($conditions)->paginate($pageSize,$this->getCol,'page',$page);
        return responseSuccess($records->items(),$records->total());
    }

    public function add(Request $request) {
        $info = $request->input();
        $this->validate($request, [

        ],[],[

        ]);
        if($this->instance->where('unit_name',$info['unit_name'])->exists()){
            return responseFail($info['unit_name'].' 已经存在！');
        }
        $record = $this->instance->updateOrCreate(['id'=>$info['id']??0],$info);
        return responseSuccess($record->toArray(),1);
    }

    public function detail(Request $request){
        $id = $request->input('id',0);
        $record = $this->instance->getRecordById($id);
        return responseSuccess($record->toArray(),1);
    }

    public function change(Request $request) {
        $id = $request->input('id',0);
        $status = $request->input('status');
        $this->instance->changeColById($id, compact('status'));
        return responseSuccess();
    }

    public function del(Request $request) {
        $id = $request->input('id',0);
        $this->instance->delRecordsByIds($id);
        return responseSuccess();
    }
}
